<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { depAll } from '@/services/consult'
import type { TopDep, SubDep } from '@/types/consult'
import { useConsultStore } from '@/stores'
const depList = ref<TopDep[]>([])
onMounted(() => {
  getList()
})
async function getList() {
  const res = await depAll()
  console.log(res)
  depList.value = res.data
}
// 得到二级科室的数据
const subList = computed<SubDep[]>(() => {
  // 如果depList没有获取到数据就得到一个空数组
  if (!depList.value.length) return []
  return depList.value[active.value].child
})
// 默认选中下标为0的项目
const active = ref(0)
// 保存科室id
const { setDepId } = useConsultStore()
function saveDepId(id: string) {
  setDepId(id)
}
</script>

<template>
  <div class="consult-dep-page">
    <cp-nav-bar title="选择科室" />
    <div class="wrapper">
      <van-sidebar v-model="active">
        <van-sidebar-item v-for="item in depList" :key="item.id" :title="item.name" />
      </van-sidebar>
      <div class="sub-dep">
        <router-link v-for="item in subList" :key="item.id" @click="saveDepId(item.id)" to="/consult/illness">{{ item.name }}</router-link>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.van-sidebar {
  width: 114px;
  &-item {
    padding: 14px;
    color: var(--cp-tag);
    &--select {
      color: var(--cp-main);
      font-weight: normal;
      &::before {
        display: none;
      }
    }
  }
}
.consult-dep-page {
  padding-top: 46px;
}
.wrapper {
  height: calc(100vh - 46px);
  overflow: hidden;
  display: flex;
  .sub-dep {
    flex: 1;
    height: 100%;
    overflow-y: auto;
    > a {
      display: block;
      padding: 14px 30px;
      color: var(--cp-dark);
    }
  }
}
</style>
